﻿using System;
using System.Threading.Tasks;
using Android.App;
using Android.Graphics;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Support.V7.App;
using CPXamarinPortable;
using Android.Views.Animations;
using Android.Util;
using Android.Text;
using com.refractored;
using Android.Support.V4.View;

namespace CPXamarin
{
    [Activity(Label = "CPXamarin",MainLauncher = true)]
    public class MainActivity : ActionBarActivity,IScrollTabHolder,ViewPager.IOnPageChangeListener
    {
        private KenBurnsSupportView mHeaderPicture;
        private View mHeader;
        private static AccelerateDecelerateInterpolator sSmoothInterpolator = new AccelerateDecelerateInterpolator();
        private int mActionBarHeight;
        private int mMinHeaderHeight;
        private int mHeaderHeight;
        private int mMinHeaderTranslation;
        private ImageView icon;
        private ImageView mHeaderLogo;
        private RectF mRect1 = new RectF();
        private RectF mRect2 = new RectF();
        private TypedValue mTypedValue = new TypedValue();
        private AlphaForegroundColorSpan mAlphaForegroundColorSpan;
        private ViewPager mViewPager;
        private PagerSlidingTabStrip mPagerSlidingTabStrip;
        private CustomTabAdapter tabAdapter;
        protected  override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            mMinHeaderHeight = Resources.GetDimensionPixelSize(Resource.Dimension.min_header_height);
            mHeaderHeight = Resources.GetDimensionPixelSize(Resource.Dimension.header_height);
            mMinHeaderTranslation = -mMinHeaderHeight + GetActionBarHeight();
            SetContentView(Resource.Layout.Main);
            mHeaderPicture = FindViewById<KenBurnsSupportView>(Resource.Id.header_picture);
            mHeaderPicture.SetResourceIds(Resource.Drawable.banner2);
            mHeaderLogo = FindViewById<ImageView>(Resource.Id.imageProfile);
            mHeader = FindViewById<FrameLayout>(Resource.Id.header);
           
            icon = FindViewById<ImageView>(Resource.Id.imageProfile);
            tabAdapter = new CustomTabAdapter(SupportFragmentManager);
            tabAdapter.SetTabHolderScrollingContent(this);
            mViewPager = FindViewById<ViewPager>(Resource.Id.pager);
            mPagerSlidingTabStrip = FindViewById<PagerSlidingTabStrip>(Resource.Id.tabs);
           
            mViewPager.Adapter = tabAdapter;

            var drawable = RoundAvatar.GetRoundDrawable(mHeaderLogo.Drawable);
            mHeaderLogo.SetImageDrawable(drawable);
            mPagerSlidingTabStrip.SetViewPager(mViewPager);
            mPagerSlidingTabStrip.OnPageChangeListener = this;
            mAlphaForegroundColorSpan = new AlphaForegroundColorSpan(Color.Transparent);

            //var t = GetActionBarIconView();
           // SupportActionBar.SetBackgroundDrawable(null);
            // SetupActionBar();

            // SupportActionBar.SetBackgroundDrawable(null);

            // var obj =  await CPAuth.MyProfile();


            // Task<Bitmap> bmp = Images.FromUrl(obj.Avatar);
            //  var bitmap = await bmp;

            // var roundedImage = new CircleDrawable(bitmap);
            // imageView.SetImageDrawable(roundedImage);

        }
       
        public int GetActionBarHeight()
        {
            if (mActionBarHeight != 0)
            {
                return mActionBarHeight;
            }
            Theme.ResolveAttribute(Resource.Attribute.actionBarSize, mTypedValue, true);
            mActionBarHeight = TypedValue.ComplexToDimensionPixelSize(mTypedValue.Data, Resources.DisplayMetrics);
            return mActionBarHeight;
        }

        private void SetTitleAlpha(float alpha)
        {
            mAlphaForegroundColorSpan.setAlpha(alpha);
        }

        /// <Docs>The options menu in which you place your items.</Docs>

        /// <returns>To be added.</returns>

        /// <summary>

        /// This is the menu for the Toolbar/Action Bar to use

        /// </summary>

        /// <param name="menu">Menu.</param>

        public override bool OnCreateOptionsMenu(IMenu menu)
        {

            MenuInflater.Inflate(Resource.Menu.home, menu);

            return base.OnCreateOptionsMenu(menu);

        }

        public override bool OnOptionsItemSelected(IMenuItem item)
        {

            Toast.MakeText(this, "Top ActionBar pressed: " + item.TitleFormatted, ToastLength.Short).Show();

            return base.OnOptionsItemSelected(item);

        }

        public void AdjustScroll(int scrollHeight)
        {
        }

        public void OnScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount, int pagePosition)
        {
            if (mViewPager.CurrentItem == pagePosition)
            {
                int scrollY = GetScrollY(view);
                //sticky actionbar
                mHeader.TranslationY = (Math.Max(-scrollY, mMinHeaderTranslation));
              
            }
        }

        public void OnPageScrollStateChanged(int state)
        {
        }

        public void OnPageScrolled(int position, float positionOffset, int positionOffsetPixels)
        {
        }

        public void OnPageSelected(int position)
        {
            SparseArray<IScrollTabHolder> scrollTabHolder = tabAdapter.GetScrollTabHolders();
            IScrollTabHolder currentHolder = scrollTabHolder.ValueAt(position);
            currentHolder.AdjustScroll((int)(mHeader.Height+mHeader.TranslationY));
        }

        public int GetScrollY(AbsListView view)
        {
            var c = view.GetChildAt(0);
            if (c == null)
            {
                return 0;
            }
            int firstVisiblePosition = view.FirstVisiblePosition;
            int top = c.Top;
            int headerHeight = 0;
            if (firstVisiblePosition >= 1)
            {
                headerHeight = mHeaderHeight;
            }
            return -top + firstVisiblePosition * c.Height + headerHeight;
        }

        private RectF GetOnScreenRect(RectF rect, View view)
        {
            rect.Set(view.Left, view.Top, view.Right, view.Bottom);
            return rect;
        }


        public static float Clamp(float value, float min, float max)
        {
            return Math.Max(min, Math.Min(value, max));
        }

    }
}

